package com.fedorvlasov.lazylist;

import java.io.InputStream;
import java.io.OutputStream;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

public class Utils {
    public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
    
    private static Account getAccount(AccountManager accountManager) {
        Account[] accounts = accountManager.getAccountsByType("com.google");
        Account account;
        if (accounts.length > 0) {
            account = accounts[0];
        } else {
            account = null;
        }
        return account;
    }
    
    

       public String getEmail(Context context) {
            AccountManager accountManager = AccountManager.get(context);
            Account account = getAccount(accountManager);

            if (account == null) {
                return null;
            } else {
                return account.name;
            }
        }
        
        public String getUsername(Context context){
        	//String email;
            AccountManager manager = AccountManager.get(context); 
            Account account = getAccount(manager);
            if (account == null) {
                return "";
            } else {
                String email =  account.name;
                String[] parts = email.split("@");
                if(parts.length > 0 && parts[0] != null)
                    return parts[0];
                else return "";
            }  
        }
}